package com.lijin.study.java.iotest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileReader {

	private static final String FILE = "sod_data.csv";

	/**
	 * Version1
	 * 
	 * @throws Exception
	 */
	public static void countFile() throws Exception {
		InputStreamReader reader = new InputStreamReader(new FileInputStream(
				new File(FILE)));
		int c = -1;
		int columnCount = 1;
		int lineCount = 1;
		boolean firstLine = true;

		while ((c = reader.read()) != -1) {
			if (firstLine) {
				if (c == ',') {
					columnCount++;
				}
			}

			if (c == '\n') {
				firstLine = false;
				lineCount++;
			}
		}

		System.out.println("Column Count : " + columnCount);
		System.out.println("Line Count : " + lineCount);
	}

	/**
	 * Version 2
	 * 
	 * @throws Exception
	 */
	public static void countFile2() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(new File(FILE))));
		String line = null;
		int lineCount = 0;
		int columnCount = 0;

		while (reader.read() != -1) {
			line = reader.readLine();
			if (lineCount == 0) {
				columnCount = line.split(",").length;
			}
			lineCount++;
		}

		System.out.println("Column Count : " + columnCount);
		System.out.println("Line Count : " + lineCount);
	}
	
	public static void countFile3(String fileNm) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(new File(fileNm))));
		int lineCount = 0;
		String line = reader.readLine();
		while (reader.ready()) {
			System.out.println(reader.ready());
			System.out.println(reader.readLine());
//			if (line.trim().equals("")) {
//				line = reader.readLine();
//				continue;
//			} else {
//				lineCount ++;
//				System.out.println(line);
//				line = reader.readLine();
//			}
			
			System.out.println(reader.ready());
		}
		System.out.println("Line Count : " + lineCount);
	}
	
	/**
	 * Read specify line and column
	 * @param line
	 * @param column
	 * @return
	 * @throws Exception
	 */
	public static String readContent(int line, int column) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(new File(FILE))));
		String result = null;
		int lineCount = 0;
		
		while (reader.read() != -1) {
			lineCount++;
			result = reader.readLine();
			if (lineCount == line) {
				break;
			}
		}
		if (line != lineCount) {
			return null;
		}
		if (result != null) {
			String[] strs = result.split(",");
			if (strs.length > column) {
				result = strs[column - 1];
			} else {
				result = null;
			}
		}
		return result;
	}

	
	public static void readFile(String fileNm) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileNm)), "GBK"));
		String line = br.readLine();
		Map<String, String> result = new HashMap<String, String>();
		while (line != null) {
			System.out.println(line);
			String[] tm = line.split("@@");
			if (result.get(tm[0]) != null) {
				result.put(tm[0], tm[1] + "|" + result.get(tm[0]));
			} else {
				result.put(tm[0], tm[1]);
			}
			System.out.println(result.get(tm[0]));
			line = br.readLine();
		}
		
		br.close();
		
		System.out.println("---" + result.size());
		
		br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("latest_half_year_2.txt")), "GBK"));
		List<String> test = new ArrayList<String>();
		line = br.readLine();
		while (line != null) {
			String mcd = line.substring(0, 15);
			test.add(mcd + "@" + result.get(mcd));
			line = br.readLine();
		}
		br.close();
		
		System.out.println("----" + test.size());
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(new File("latest_half_year_result.txt")));
		for (String t : test) {
			bw.write(t + "\n");
		}
		bw.flush();
		bw.close();
	}
	
	
	public static void main(String[] args) throws Exception {
		countFile3("TARGET");
	}
}
